package com.jz.jingying.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jz.jingying.bean.*;
import com.jz.jingying.mapper.sqlServer.*;
import com.jz.jingying.service.JingYingToJiayingService;
import com.jz.netthings.appengine.baseData.ProviderInfo;
import com.jz.netthings.appengine.mapper.ProviderInfoMapper;
import com.jz.netthings.application.bodyCheck.bean.BodyCheck;
import com.jz.netthings.application.bodyCheck.mapper.mysql.BodyCheckMapper;
import com.jz.netthings.application.providerCollection.bean.ProviderCollection;
import com.jz.netthings.application.providerCollection.mapper.mysql.ProviderCollectionMapper;
import com.jz.netthings.application.providerCost.bean.ProviderCost;
import com.jz.netthings.application.providerCost.mapper.mysql.ProviderCostMapper;
import com.jz.netthings.application.providerCreate.bean.ProviderXp;
import com.jz.netthings.application.providerCreate.bean.ProviderZw;
import com.jz.netthings.application.providerCreate.mapper.mysql.ProviderXpMapper;
import com.jz.netthings.application.providerCreate.mapper.mysql.ProviderZwMapper;
import com.jz.netthings.application.providerReg.bean.ProviderReg;
import com.jz.netthings.application.providerReg.mapper.mysql.ProviderRegMapper;
import com.jz.netthings.application.sample.bean.PlasmaSamples;
import com.jz.netthings.application.sample.mapper.PlasmaSamplesMapper;
import com.jz.netthings.application.upload.service.UploadService;
import com.jz.netthings.utils.AdscmBaseUtils;
import com.jz.netthings.utils.AdscmDateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author zyl
 * @Date 2019/4/17 -14:04
 */
@Service
public class JingyingToJiaYingServicerImpl implements JingYingToJiayingService {
    @Autowired
    BodyCheckMapper bodyCheckMapper;
    @Autowired
    TBodycheckrecordMapper tBodycheckrecordMapper;
    @Autowired
    ProviderInfoMapper providerInfoMapper;
    @Autowired
    PlasmaSamplesMapper plasmaSamplesMapper;
    @Autowired
    TAssayrecordMapper tAssayrecordMapper;
    @Autowired
    ProviderRegMapper providerRegMapper;
    Logger logger = Logger.getLogger(JingyingToJiaYingServicerImpl.class);
    @Autowired
    TProviderbaseinfoMapper tProviderbaseinfoMapper;
    @Autowired
    UploadService uploadService;
    @Value("${ly.upload.baseUrl}")
    String  photoUrl;
    @Autowired
    TProviderregisterMapper tProviderregisterMapper;
    @Autowired
    TZwtableMapper tZwtableMapper;
    @Autowired
    ProviderZwMapper providerZwMapper;
    @Autowired
    ProviderXpMapper providerXpMapper;
    @Autowired
    TBodychecklastrecordMapper tBodychecklastrecordMapper;
    @Autowired
    TCollectionrecordMapper tCollectionrecordMapper;
    @Autowired
    ProviderCollectionMapper providerCollectionMapper;
    @Autowired
    ProviderCostMapper providerCostMapper;
    @Autowired
    TRaterecordMapper tRaterecordMapper;

    @Override
    public void providerinfoDataImport() {
        //首先是查询出总条数
        int counts = tProviderbaseinfoMapper.selectCountProviderInfo();
        //设置为50条未一页；
        int i = counts / 50;
        int temp = counts % 50;
        if (temp>0){
            i= i+1;
        }
        String id ="";
        String cardId="";
        String years = new SimpleDateFormat("yy").format(new Date());
        Wrapper<ProviderInfo> providerInfoWrapper=new EntityWrapper<>();
        providerInfoWrapper.like("id",years, SqlLike.RIGHT);
        providerInfoWrapper.orderBy("id",false);
        List<ProviderInfo> providerInfos = providerInfoMapper.selectList(providerInfoWrapper);
        if (providerInfos.size()>0){
            ProviderInfo providerInfo1 = providerInfos.get(0);
            id= String.valueOf(providerInfo1.getId()+1)  ;
        }else {
            id=years+"00000";
        }
        Long longId=Long.valueOf(id);
        for (int j = 0; j <=i; j++) {

            List<TProviderbaseinfo> tProviderbaseinfos = tProviderbaseinfoMapper.selectProviderInfo(50, j*50);
            for (TProviderbaseinfo tProviderbaseinfo : tProviderbaseinfos) {
                ProviderInfo providerInfo = new ProviderInfo();
                //先到数据库查询是否有相同的卡号数据
                tProviderbaseinfo.getProviderNo();
                Wrapper<ProviderInfo> getSameProviderInfo=new EntityWrapper<>();
                getSameProviderInfo.eq("idNo",tProviderbaseinfo.getIDNo());
                List<ProviderInfo> getSameProviderInfos = providerInfoMapper.selectList(getSameProviderInfo);
                if (getSameProviderInfos!=null &&getSameProviderInfos.size()>0){
                    logger.info("查询到有相同的卡号基本信息 修改："+tProviderbaseinfo.getProviderNo());
                    providerInfo=getSameProviderInfos.get(0);
                    providerInfo.setBirthday(AdscmDateUtils.parseDateL(tProviderbaseinfo.getBirthDay()));
                    providerInfo.setAge(AdscmDateUtils.getAge(AdscmDateUtils.parseDateL(tProviderbaseinfo.getBirthDay())));
                    providerInfo.setCardId(tProviderbaseinfo.getProviderNo());
                    providerInfo.setName(tProviderbaseinfo.getName());
                    providerInfo.setIdNo(tProviderbaseinfo.getIDNo());
                    providerInfo.setIdNoAddress(tProviderbaseinfo.getAddress());
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getNowAddress(),null)){
                        providerInfo.setAddress(tProviderbaseinfo.getNowAddress());
                    }else {
                        providerInfo.setAddress(tProviderbaseinfo.getAddress());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getTel(),null)){
                        providerInfo.setPhone(tProviderbaseinfo.getTel());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getBloodType(),null)){
                        if ("A".equals(tProviderbaseinfo.getBloodType())){
                            providerInfo.setBloodType("1");
                        }
                        if ("B".equals(tProviderbaseinfo.getBloodType())){
                            providerInfo.setBloodType("2");
                        }
                        if ("AB".equals(tProviderbaseinfo.getBloodType())){
                            providerInfo.setBloodType("3");
                        }
                        if ("O".equals(tProviderbaseinfo.getBloodType())){
                            providerInfo.setBloodType("4");
                        }
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getIsMarried(),null)){
                        //   if (tProviderbaseinfo.getIsMarried()){
                        providerInfo.setMaritalStatus("1");
                    }else {
                        providerInfo.setMaritalStatus("0");
                    }


                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getNation(),null)){
                        providerInfo.setNation( Integer.valueOf(tProviderbaseinfo.getNation()) );
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getProviderState(),null)){
                        if ( "0".equals( tProviderbaseinfo.getProviderState()) ){
                            providerInfo.setProviderState(2);
                        }
                        if ("1".equals(tProviderbaseinfo.getProviderState())){
                            providerInfo.setProviderState(0);
                        }
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getFileDate(),null)){
                        providerInfo.setCreateDate(AdscmDateUtils.parseDateL(tProviderbaseinfo.getFileDate()) );
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getTimes(),null)){
                        providerInfo.setProvideTotalTimes( Integer.valueOf(tProviderbaseinfo.getTimes()));
                    }

                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getRefuseDate(),null)){
                        providerInfo.setRefuseDate(AdscmDateUtils.parseDateL(tProviderbaseinfo.getRefuseDate()));
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getRefuseReason(),null)){
                        providerInfo.setRefuseReason(tProviderbaseinfo.getRefuseReason());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getCollectionDate(),null)){
                        providerInfo.setLastDate(AdscmDateUtils.parseDateL( tProviderbaseinfo.getCollectionDate()));
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getCollectionDate(),null)){
                        providerInfo.setLastDate(AdscmDateUtils.parseDateL(tProviderbaseinfo.getCollectionDate()));
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getSFZ_DQ(),null)){
                        providerInfo.setIdCardValidDate(AdscmDateUtils.parseDateL(tProviderbaseinfo.getSFZ_DQ()));
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getSex(),null)){
                        if ("F".equals(tProviderbaseinfo.getSex())){
                            providerInfo.setSex("2");
                        }
                        if ("M".equals(tProviderbaseinfo.getSex())){
                            providerInfo.setSex("1");
                        }

                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getPhoto(),null)){
                        if (AdscmBaseUtils.getInstance().isNotEmpty(providerInfo.getPhoto(),null)){
                            uploadService.deleteImage(photoUrl + providerInfo.getPhoto());
                        }
                        byte[]   photos = tProviderbaseinfo.getPhoto();
                        String jpg = uploadService.uploadImageByBytes(photos);
                        providerInfo.setPhoto(jpg.replace(photoUrl,""));
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getIDCardPhoto(),null)){
                        if (AdscmBaseUtils.getInstance().isNotEmpty(providerInfo.getIdCardPhoto(),null)){
                            uploadService.deleteImage(photoUrl + providerInfo.getIdCardPhoto());
                        }
                        byte[]   photos = tProviderbaseinfo.getIDCardPhoto();
                        String jpg = uploadService.uploadImageByBytes(photos);
                        providerInfo.setIdCardPhoto(jpg.replace(photoUrl,""));
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getIDCardPhotoBlack(),null)){
                        //删除旧的
                        if (AdscmBaseUtils.getInstance().isNotEmpty(providerInfo.getIdCardPhoto1(),null)){
                            uploadService.deleteImage(photoUrl + providerInfo.getIdCardPhoto1());
                        }
                        byte[]   photos = tProviderbaseinfo.getIDCardPhotoBlack();
                        String jpg = uploadService.uploadImageByBytes(photos);
                        providerInfo.setIdCardPhoto1(jpg.replace(photoUrl,""));
                    }
                    providerInfoMapper.updateById(providerInfo);

                }else {
                    providerInfo.setId(longId=longId+1);
                    providerInfo.setBirthday(AdscmDateUtils.parseDateL(tProviderbaseinfo.getBirthDay()));
                    providerInfo.setAge(AdscmDateUtils.getAge(AdscmDateUtils.parseDateL(tProviderbaseinfo.getBirthDay())));
                    providerInfo.setCardId(tProviderbaseinfo.getProviderNo());
                    providerInfo.setName(tProviderbaseinfo.getName());
                    providerInfo.setIdNo(tProviderbaseinfo.getIDNo());
                    providerInfo.setIdNoAddress(tProviderbaseinfo.getAddress());
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getNowAddress(),null)){
                        providerInfo.setAddress(tProviderbaseinfo.getNowAddress());
                    }else {
                        providerInfo.setAddress(tProviderbaseinfo.getAddress());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getTel(),null)){
                        providerInfo.setPhone(tProviderbaseinfo.getTel());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getBloodType(),null)){
                        if ("A".equals(tProviderbaseinfo.getBloodType())){
                            providerInfo.setBloodType("0");
                        }
                        if ("B".equals(tProviderbaseinfo.getBloodType())){
                            providerInfo.setBloodType("2");
                        }
                        if ("AB".equals(tProviderbaseinfo.getBloodType())){
                            providerInfo.setBloodType("3");
                        }
                        if ("O".equals(tProviderbaseinfo.getBloodType())){
                            providerInfo.setBloodType("4");
                        }
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getIsMarried(),null)) {
                        if ("1".equals(tProviderbaseinfo.getIsMarried())) {
                            providerInfo.setMaritalStatus("1");
                        } else {
                            providerInfo.setMaritalStatus("0");
                        }
                    }

                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getNation(),null)){
                        providerInfo.setNation( Integer.valueOf(tProviderbaseinfo.getNation()) );
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getProviderState(),null)){
                        if ( "0".equals( tProviderbaseinfo.getProviderState()) ){
                            providerInfo.setProviderState(2);
                        }
                        if ("1".equals(tProviderbaseinfo.getProviderState())){
                            providerInfo.setProviderState(0);
                        }
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getFileDate(),null)){
                        providerInfo.setCreateDate(AdscmDateUtils.parseDateL(tProviderbaseinfo.getFileDate()) );
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getTimes(),null)){
                        providerInfo.setProvideTotalTimes( Integer.valueOf(tProviderbaseinfo.getTimes()));
                    }

                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getRefuseDate(),null)){
                        providerInfo.setRefuseDate(AdscmDateUtils.parseDateL(tProviderbaseinfo.getRefuseDate()));
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getRefuseReason(),null)){
                        providerInfo.setRefuseReason(tProviderbaseinfo.getRefuseReason());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getCollectionDate(),null)){
                        providerInfo.setLastDate(AdscmDateUtils.parseDateL( tProviderbaseinfo.getCollectionDate()));
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getCollectionDate(),null)){
                        providerInfo.setLastDate(AdscmDateUtils.parseDateL(tProviderbaseinfo.getCollectionDate()));
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getSFZ_DQ(),null)){
                        providerInfo.setIdCardValidDate(AdscmDateUtils.parseDateL(tProviderbaseinfo.getSFZ_DQ()));
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getSex(),null)){
                        if ("F".equals(tProviderbaseinfo.getSex())){
                            providerInfo.setSex("2");
                        }
                        if ("M".equals(tProviderbaseinfo.getSex())){
                            providerInfo.setSex("1");
                        }
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getRegister(),null)){
                        providerInfo.setCreateNurse(tProviderbaseinfo.getRegister());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getPhoto(),null)){
                        byte[]   photos = tProviderbaseinfo.getPhoto();
                        String jpg = uploadService.uploadImageByBytes(photos);
                        providerInfo.setPhoto(jpg.replace(photoUrl,""));
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getIDCardPhoto(),null)){
                        byte[]   photos = tProviderbaseinfo.getIDCardPhoto();
                        String jpg = uploadService.uploadImageByBytes(photos);
                        providerInfo.setIdCardPhoto(jpg.replace(photoUrl,""));
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderbaseinfo.getIDCardPhotoBlack(),null)){
                        byte[]   photos = tProviderbaseinfo.getIDCardPhotoBlack();
                        String jpg = uploadService.uploadImageByBytes(photos);
                        providerInfo.setIdCardPhoto1(jpg.replace(photoUrl,""));
                    }
                    providerInfoMapper.insert(providerInfo);
                }

            }

        }

    }
    @Override
    public void proivderZwDataImport() {
        int counts = tZwtableMapper.selectCountTZwtable();
        //设置为50条未一页；
        int i = counts / 50;
        int temp = counts % 50;
        if (temp>0){
            i= i+1;
        }
        for (int j = 0; j <=i; j++) {
            List<TZwtable> tZwtables = tZwtableMapper.selectTZwtable(50, j * 50);
            for (TZwtable tZwtable : tZwtables) {
                //根据卡号查询到这个
                Wrapper<ProviderInfo> wrapper =new EntityWrapper<>();
                wrapper.eq("cardId",  tZwtable.getProviderno());
                List<ProviderInfo> providerInfos = providerInfoMapper.selectList(wrapper);
                if (providerInfos.size()>0){
                    ProviderInfo providerInfo = providerInfos.get(0);
                    //查询是否存在了这个指纹
                    Wrapper<ProviderZw> providerZwWrapper=new EntityWrapper<>();
                    providerZwWrapper.eq("providerNo",providerInfo.getId());
                    providerZwWrapper.eq("code",tZwtable.getCode());
                    List<ProviderZw> providerZws = providerZwMapper.selectList(providerZwWrapper);
                    if (providerZws!=null && providerZws.size()>0){
                        ProviderZw providerZw = providerZws.get(0);
                        providerZw.setZw(tZwtable.getZW());
                        Wrapper<ProviderZw> wrapper1 = new EntityWrapper<>();
                        wrapper1.eq("providerNo",providerZw.getProviderNo());
                        wrapper1.eq("code",providerZw.getCode());
                        providerZwMapper.update(providerZw,wrapper1);//.updateById(providerZw);
                    }else {

                        ProviderZw providerZw =new ProviderZw();
                        providerZw.setProviderNo(providerInfo.getId());
                        providerZw.setZw(tZwtable.getZW());
                        providerZw.setCode(tZwtable.getCode());
                        providerZwMapper.insert(providerZw);
                    }
                }
            }

        }
    }

    @Override
    public void proivderXpDataImport() {
        int counts = tBodychecklastrecordMapper.selectTBodychecklastrecordCount();
        //设置为50条未一页；
        int i = counts / 50;
        int temp = counts % 50;
        if (temp>0){
            i= i+1;
        }
        for (int j = 0; j <=i; j++) {
            List<TBodychecklastrecord> tBodychecklastrecords = tBodychecklastrecordMapper.selectTBodychecklastrecord(50, j * 50);
            for (TBodychecklastrecord tBodychecklastrecord : tBodychecklastrecords) {
                Wrapper<ProviderInfo> wrapper =new EntityWrapper<>();
                wrapper.eq("cardId",  tBodychecklastrecord.getProviderNo());
                List<ProviderInfo> providerInfos = providerInfoMapper.selectList(wrapper);
                if (providerInfos.size()>0){
                    ProviderInfo providerInfo = providerInfos.get(0);
                    //查询是否存在这个胸片记录了
                    Wrapper<ProviderXp> providerXpWrapper =new EntityWrapper<>();
                    providerXpWrapper.eq("providerNo",providerInfo.getId());
                    providerXpWrapper.eq("createDate",tBodychecklastrecord.getLastbodycheckdate());
                    List<ProviderXp> providerXps = providerXpMapper.selectList(providerXpWrapper);
                    if (providerXps!=null &&providerXps.size()>0 ){
                        ProviderXp providerXp = providerXps.get(0);
                        providerXp.setProviderNo(providerInfo.getId().toString());
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tBodychecklastrecord.getXTYS(),null)){
                            providerXp.setDoctor(tBodychecklastrecord.getXTYS());
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tBodychecklastrecord.getXTSJ(),null)){
                            providerXp.setCreateDate( AdscmDateUtils.parseDateL(tBodychecklastrecord.getXTSJ()) );
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tBodychecklastrecord.getLastbodycheckdate(),null)){
                            providerXp.setValidDate(  AdscmDateUtils.getThatDate( AdscmDateUtils.parseDateL(tBodychecklastrecord.getLastbodycheckdate()),1, Calendar.YEAR));
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tBodychecklastrecord.getXTDW(),null)){
                            providerXp.setHospital(tBodychecklastrecord.getXTDW());
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tBodychecklastrecord.getLastbodychecker(),null)){
                            providerXp.setCrearor(tBodychecklastrecord.getLastbodychecker().toString());
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tBodychecklastrecord.getXTGG(),null)){
                            if ("合格".equals(tBodychecklastrecord.getXTGG())){
                                providerXp.setResult("1");
                            }
                            if ("不合格".equals(tBodychecklastrecord.getXTGG())){
                                providerXp.setResult("0");
                            }

                        }
                        providerXpMapper.updateById(providerXp);

                    }else {
                        ProviderXp providerXp=new ProviderXp();
                        providerXp.setProviderNo(providerInfo.getId().toString());
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tBodychecklastrecord.getXTYS(),null)){
                            providerXp.setDoctor(tBodychecklastrecord.getXTYS());
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tBodychecklastrecord.getXTSJ(),null)){
                            providerXp.setCreateDate( AdscmDateUtils.parseDateL(tBodychecklastrecord.getXTSJ()) );
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tBodychecklastrecord.getLastbodycheckdate(),null)){
                            providerXp.setValidDate(  AdscmDateUtils.getThatDate( AdscmDateUtils.parseDateL(tBodychecklastrecord.getLastbodycheckdate()),1, Calendar.YEAR));
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tBodychecklastrecord.getXTDW(),null)){
                            providerXp.setHospital(tBodychecklastrecord.getXTDW());
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tBodychecklastrecord.getLastbodychecker(),null)){
                            providerXp.setCrearor(tBodychecklastrecord.getLastbodychecker().toString());
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tBodychecklastrecord.getXTGG(),null)){
                            if ("合格".equals(tBodychecklastrecord.getXTGG())){
                                providerXp.setResult("1");
                            }
                            if ("不合格".equals(tBodychecklastrecord.getXTGG())){
                                providerXp.setResult("0");
                            }

                        }
                        providerXpMapper.insert(providerXp);
                    }

                }
            }
        }
    }

    @Override
    public void providerRegDataImpor() {
        int counts = tProviderregisterMapper.selectCount();
        //设置为50条未一页；
        int i = counts / 50;
        int temp = counts % 50;
        if (temp > 0) {
            i = i + 1;
        }
        for (int j = 0; j <= i; j++) {
            List<TProviderregister> tProviderregisters = tProviderregisterMapper.selectTProviderregister(50, j * 50);
            for (TProviderregister tProviderregister : tProviderregisters) {
                //查询是否已经存在了对应的登记记录
                ProviderReg providerReg = new ProviderReg();
                Wrapper<ProviderReg> getSameProviderReg = new EntityWrapper<>();
                getSameProviderReg.eq("registerNo", tProviderregister.getSmallNo());
                List<ProviderReg> providerRegs = providerRegMapper.selectList(getSameProviderReg);
                Wrapper<ProviderInfo> wrapper = new EntityWrapper<>();
                wrapper.eq("cardId", tProviderregister.getProviderNo());
                List<ProviderInfo> providerInfos = providerInfoMapper.selectList(wrapper);
                if (providerInfos.size() > 0) {
                    ProviderInfo providerInfo = providerInfos.get(0);
                    providerReg.setProviderNo(providerInfo.getId().toString());
                }
                if (providerRegs != null && providerRegs.size() > 0) {
                    providerReg = providerRegs.get(0);
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getSmallNo(), null)) {
                        providerReg.setRegisterNo(tProviderregister.getSmallNo());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getIsBodyPass(), null)) {
                        if ("1".equals(tProviderregister.getIsBodyPass())) {
                            providerReg.setBodyCheckPass(true);
                        } else {
                            providerReg.setBodyCheckPass(false);
                        }
                    }

                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getIsAssayPass(), null)) {

                        if ("1".equals(tProviderregister.getIsAssayPass())) {
                            providerReg.setAssayPass(true);
                        } else {
                            providerReg.setAssayPass(false);
                        }
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getRegistDate(), null)) {
                        providerReg.setRegTime(AdscmDateUtils.parseDateL(tProviderregister.getRegistDate()));
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getRegistWorker(), null)) {
                        providerReg.setRegNurse(tProviderregister.getRegistWorker().toString());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getIF_GDJY(), null)) {

                        if ("1".equals(tProviderregister.getIF_GDJY())) {
                            providerReg.setIfGdjy(true);
                        } else {
                            providerReg.setIfGdjy(false);
                        }
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getXYH_SmallNo(), null)) {
                        providerReg.setSampleNo(tProviderregister.getXYH_SmallNo());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getYyisvalidate(), null)) {
                        if ("1".equals(tProviderregister.getYyisvalidate())) {
                            providerReg.setYq(true);
                        } else {
                            providerReg.setYq(false);
                        }
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getIDNo(), null)) {
                        providerReg.setIdNo(tProviderregister.getIDNo());
                    }
                    providerRegMapper.updateById(providerReg);

                } else {
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getSmallNo(), null)) {
                        providerReg.setRegisterNo(tProviderregister.getSmallNo());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getIsBodyPass(), null)) {
                        if ("1".equals(tProviderregister.getIsBodyPass())) {
                            providerReg.setBodyCheckPass(true);
                        } else {
                            providerReg.setBodyCheckPass(false);
                        }
                    }

                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getIsAssayPass(), null)) {

                        if ("1".equals(tProviderregister.getIsAssayPass())) {
                            providerReg.setAssayPass(true);
                        } else {
                            providerReg.setAssayPass(false);
                        }
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getRegistDate(), null)) {
                        providerReg.setRegTime(AdscmDateUtils.parseDateL(tProviderregister.getRegistDate()));
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getRegistWorker(), null)) {
                        providerReg.setRegNurse(tProviderregister.getRegistWorker().toString());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getIF_GDJY(), null)) {

                        if ("1".equals(tProviderregister.getIF_GDJY())) {
                            providerReg.setIfGdjy(true);
                        } else {
                            providerReg.setIfGdjy(false);
                        }
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getXYH_SmallNo(), null)) {
                        providerReg.setSampleNo(tProviderregister.getXYH_SmallNo());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getYyisvalidate(), null)) {
                        if ("1".equals(tProviderregister.getYyisvalidate())) {
                            providerReg.setYq(true);
                        } else {
                            providerReg.setYq(false);
                        }
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tProviderregister.getIDNo(), null)) {
                        providerReg.setIdNo(tProviderregister.getIDNo());
                    }
                    providerRegMapper.insert(providerReg);
                }


            }

        }
    }

    @Override
    public void bodyCheckDataImport() throws Exception {
        Integer counts= tBodycheckrecordMapper.queryCount();
        int i = counts / 50;
        int temp = counts % 50;
        if (temp>0){
            i= i+1;
        }
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        for (int j = 0; j <= i; j++) {
            List<TBodycheckrecord> query = tBodycheckrecordMapper.query(j, j * 50);
            for (TBodycheckrecord tb : query) {
                BodyCheck bc=new BodyCheck();
                if (AdscmBaseUtils.getInstance().isNotEmpty(tb.getProviderNo(),null)){
                    Wrapper<ProviderInfo> wrapper=new EntityWrapper<>();
                    wrapper.eq("cardId",tb.getProviderNo());
                    List<ProviderInfo> providerInfos = providerInfoMapper.selectList(wrapper);
                    if (providerInfos.size()>0){
                        bc.setProviderNo(String.valueOf(providerInfos.get(0).getId()));
                    }
            }
                bc.setCreateNurse(String.valueOf(tb.getBodyCheckWorker()));
                bc.setBodyCheckWorker(String.valueOf(tb.getBodyCheckWorker()));
                bc.setBodyCheckWorker(String.valueOf(tb.getAssessor()));
                bc.setBodyCheckNo(String.valueOf(tb.getBodyCheckNo()));
                bc.setRecodeCreateTime(tb.getBodyCheckDate());
                bc.setRegisterNo(tb.getSmallNo());
                bc.setRefuseReason(tb.getDocResult());
                bc.setRecodeStatus(1);
                if (AdscmBaseUtils.getInstance().isNotEmpty(tb.getFB(),null)){
                    if ("正常".equals(tb.getFB())){
                        bc.setAbdomen(1);
                    }else {
                        bc.setAbdomen(0);
                    }
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(tb.getPF(),null)){
                    if("正常".equals(tb.getPF())){
                        bc.setSkin(1);
                    }else {
                        bc.setSkin(0);
                    }

                }

                if (AdscmBaseUtils.getInstance().isNotEmpty(tb.getSZ(),null)){
                    if("正常".equals(tb.getSZ())){
                        bc.setLimbs(1);
                    }else {
                        bc.setLimbs(0);
                    }

                }

                if (AdscmBaseUtils.getInstance().isNotEmpty(tb.getXB(),null)){
                    if("正常".equals(tb.getXB())){
                        bc.setChest(1);
                    }else {
                        bc.setChest(0);
                    }

                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(tb.getWG(),null)){
                    if("正常".equals(tb.getWG())){
                        bc.setFacial(1);
                    }else {
                        bc.setFacial(0);
                    }

                }

                bc.setAnimalHeat(tb.getAnimalHeat());
                if (AdscmBaseUtils.getInstance().isNotEmpty(tb.getBodyCheckResult(),null)){
                    if (tb.getBodyCheckResult().equals("1")){
                        bc.setBodyCheckResult(1);
                    }else {
                        bc.setBodyCheckResult(0);
                    }
                }

                if(AdscmBaseUtils.getInstance().isNotEmpty(tb.getBloodPressure(),null)){
                    bc.setHighBlood(tb.getBloodPressure().split("/")[0]);
                    bc.setLowBlood(tb.getBloodPressure().split("/")[1]);
                }

                bc.setWeight(tb.getAvoirdupois());
                bc.setOutReason(tb.getOtherRefusereason());
                bc.setAuthMethod(1);
                bc.setPulse(tb.getPulse());
                bodyCheckMapper.insert(bc);

            }
        }
    }

    @Override
    public void assayRecordDataImport() throws Exception {
        Integer counts= tAssayrecordMapper.queryCount();
        int i = counts / 50;
        int temp = counts % 50;
        if (temp>0){
            i= i+1;
        }
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        for (int j = 0; j <=i; j++) {
            List<TAssayrecord> query = tAssayrecordMapper.query(50, j*50);
            for (TAssayrecord tAssayrecord : query) {

                PlasmaSamples plas=new PlasmaSamples();
                plas.setCardId(tAssayrecord.getProviderNo());
                plas.setCreateNurse(String.valueOf(tAssayrecord.getAssayWorker()));
                if (AdscmBaseUtils.getInstance().isNotEmpty(tAssayrecord.getAssayResult(),null)){
                    if ("1".equals(tAssayrecord.getAssayResult())){
                        plas.setInspectionResult(1);
                    }else {
                        plas.setInspectionResult(0);
                    }
                }

                plas.setInspectionRecodePerson(String.valueOf(tAssayrecord.getAssayWorker()));
                if (AdscmBaseUtils.getInstance().isNotEmpty(tAssayrecord.getZSYKDZ(),null)){
                    plas.setZsykdz(Float.valueOf(tAssayrecord.getZSYKDZ()));
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(tAssayrecord.getProviderNo(),null)){
                    Wrapper<ProviderInfo> wrapper=new EntityWrapper<>();
                    wrapper.eq("cardId",tAssayrecord.getProviderNo());
                    List<ProviderInfo> providerInfos = providerInfoMapper.selectList(wrapper);
                    if (providerInfos.size()>0){
                        plas.setProviderNo(String.valueOf(providerInfos.get(0).getId()));
                        plas.setBloodType(providerInfos.get(0).getBloodType());
                    }
                }
                plas.setALt(tAssayrecord.getALT());
//            plas.setBloodType(tAssayrecord.getAllBlood());
                if (AdscmBaseUtils.getInstance().isNotEmpty(tAssayrecord.getAssayDate(),null)){
                    plas.setRecodeTime(df.parse(tAssayrecord.getAssayDate()));
                    plas.setInspectionStartTime(df.parse(tAssayrecord.getAssayDate()));
                }
                plas.setHBsAg(tAssayrecord.getHBsAg());
                plas.setHcv(tAssayrecord.getHCV());
                plas.setHiv(tAssayrecord.getHIV());
                plas.setIdNo(tAssayrecord.getIDNo());
                plas.setGatheringPlace(0);
                plas.setInspectionRemark(tAssayrecord.getRemark());
                plas.setSampleNo(tAssayrecord.getXYH_SmallNo());
                plas.setRegisterNo(tAssayrecord.getSmallNo());
                plas.setHemoglobinLevel(tAssayrecord.getLSTJG());
                plas.setProteinLevel(tAssayrecord.getDBZJG());
                plas.setSerumProteinLevel(tAssayrecord.getProteide());
                plas.setSyphilis(tAssayrecord.getSyphilis());
                plas.setWholeBloodLevel(tAssayrecord.getAllBlood());
                plas.setSampleType(Integer.valueOf(tAssayrecord.getIfx()));
                if (AdscmBaseUtils.getInstance().isNotEmpty(tAssayrecord.getYMShang(),null)){
                    if ("1".equals(tAssayrecord.getYMShang())){
                        plas.setYm(1);
                    }else {
                        plas.setYm(0);
                    }
                }

                if (AdscmBaseUtils.getInstance().isNotEmpty(tAssayrecord.getYMXia(),null)){
                    if ("1".equals(tAssayrecord.getYMXia())){
                        plas.setYm(1);
                    }else {
                        plas.setYm(0);
                    }
                }
                plas.setRecodeStatus(1);
                plasmaSamplesMapper.insert(plas);
            }
        }
    }

    @Override
    public void providerCollectionDataImport() {

        int counts = tCollectionrecordMapper.selectTCollectionCount();
        //设置为50条未一页；
        int i = counts / 50;
        int temp = counts % 50;
        if (temp>0){
            i= i+1;
        }
        for (int j = 0; j <=i; j++) {
            List<TCollectionrecord> tCollectionrecords = tCollectionrecordMapper.selectTCollectionrecord(50, j * 50);
            for (TCollectionrecord tCollectionrecord : tCollectionrecords) {
                Wrapper<ProviderInfo> wrapper =new EntityWrapper<>();
                wrapper.eq("cardId",  tCollectionrecord.getProviderNo());
                List<ProviderInfo> providerInfos = providerInfoMapper.selectList(wrapper);
                if (providerInfos.size()>0){
                    ProviderInfo providerInfo = providerInfos.get(0);
                    //查询我们数据库是否已经存在了
                    Wrapper<ProviderCollection> providerCollectionWrapper=new EntityWrapper<>();
                    providerCollectionWrapper.eq("providerNo",providerInfo.getId());
                    providerCollectionWrapper.eq("registerNo",tCollectionrecord.getSmallNo());
                    List<ProviderCollection> providerCollections = providerCollectionMapper.selectList(providerCollectionWrapper);
                    if (providerCollections!=null &&providerCollections.size()>0){
                        ProviderCollection providerCollection = providerCollections.get(0);
                        providerCollection.setProviderNo(providerInfo.getId().toString());
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getIDNo(),null)){
                            providerCollection.setIdNo(tCollectionrecord.getIDNo());
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getSmallNo(),null)){
                            providerCollection.setRegisterNo(tCollectionrecord.getSmallNo());
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getCollectionWorker(),null)){
                            providerCollection.setNurse(tCollectionrecord.getCollectionWorker().toString());
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getCollectionDate(),null)){
                            providerCollection.setCollectionDate( AdscmDateUtils.parseDateL( tCollectionrecord.getCollectionDate()));
                        }

                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getIsCollection(),null)){
                            providerCollection.setIsDeal(tCollectionrecord.getIsCollection());
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getPlasmaGross(),null)){
                            providerCollection.setTotalPlasmaNum( Integer.valueOf( tCollectionrecord.getPlasmaGross()));
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getRunTime(),null)){
                            providerCollection.setCollTaken(Long.valueOf(tCollectionrecord.getRunTime())*60*1000);
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getAnticoagulant(),null)){
                            providerCollection.setUseKngNum( Float.valueOf(tCollectionrecord.getAnticoagulant()));
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getBloodGross(),null)){
                            providerCollection.setTotalBloodNum(Integer.valueOf(tCollectionrecord.getBloodGross()));
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getMachineNo(),null)){
                            providerCollection.setMachineNo(tCollectionrecord.getMachineNo());
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getCycleTimes(),null)){
                            providerCollection.setCycles( Integer.valueOf( tCollectionrecord.getCycleTimes()));
                        }
                        providerCollectionMapper.updateById(providerCollection);
                    }else {
                        ProviderCollection providerCollection =new ProviderCollection();
                        providerCollection.setProviderNo(providerInfo.getId().toString());
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getIDNo(),null)){
                            providerCollection.setIdNo(tCollectionrecord.getIDNo());
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getSmallNo(),null)){
                            providerCollection.setRegisterNo(tCollectionrecord.getSmallNo());
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getCollectionWorker(),null)){
                            providerCollection.setNurse(tCollectionrecord.getCollectionWorker().toString());
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getCollectionDate(),null)){
                            providerCollection.setCollectionDate( AdscmDateUtils.parseDateL( tCollectionrecord.getCollectionDate()));
                        }

                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getIsCollection(),null)){
                            providerCollection.setIsDeal(tCollectionrecord.getIsCollection());
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getPlasmaGross(),null)){
                            providerCollection.setTotalPlasmaNum( Integer.valueOf( tCollectionrecord.getPlasmaGross()));
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getRunTime(),null)){
                            providerCollection.setCollTaken(Long.valueOf(tCollectionrecord.getRunTime())*60*1000);
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getAnticoagulant(),null)){
                            providerCollection.setUseKngNum( Float.valueOf(tCollectionrecord.getAnticoagulant()));
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getBloodGross(),null)){
                            providerCollection.setTotalBloodNum(Integer.valueOf(tCollectionrecord.getBloodGross()));
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getMachineNo(),null)){
                            providerCollection.setMachineNo(tCollectionrecord.getMachineNo());
                        }
                        if (AdscmBaseUtils.getInstance().isNotEmpty(tCollectionrecord.getCycleTimes(),null)){
                            providerCollection.setCycles( Integer.valueOf( tCollectionrecord.getCycleTimes()));
                        }
                        providerCollectionMapper.insert(providerCollection);
                    }

                }
            }
        }

    }

    @Override
    public void providerCostDataImport() {
        int counts = tRaterecordMapper.selectTRaterecordCounts();
        //设置为50条未一页；
        int i = counts / 50;
        int temp = counts % 50;
        if (temp>0){
            i= i+1;
        }
        for (int j = 0; j <=i; j++) {
            List<TRaterecord> tRaterecords = tRaterecordMapper.selectTRaterecord(50, j * 50);
            for (TRaterecord tRaterecord : tRaterecords) {
                Wrapper<ProviderInfo> wrapper =new EntityWrapper<>();
                wrapper.eq("cardId",  tRaterecord.getProviderNo());
                List<ProviderInfo> providerInfos = providerInfoMapper.selectList(wrapper);
                if (providerInfos.size()>0){
                    ProviderInfo providerInfo = providerInfos.get(0);
                    ProviderCost providerCost=new ProviderCost();
                    providerCost.setProviderNo(providerInfo.getId().toString());
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tRaterecord.getSmallNo(),null)){
                        providerCost.setRegisterNo(tRaterecord.getSmallNo());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tRaterecord.getIDNo(),null)){
                        providerCost.setIdNo(tRaterecord.getIDNo());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tRaterecord.getCollectionDate(),null)){
                        providerCost.setCollectionDate(AdscmDateUtils.parseDateL(tRaterecord.getCollectionDate()));
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tRaterecord.getPutOutDate(),null)){
                        providerCost.setPutOutDate(AdscmDateUtils.parseDateL(tRaterecord.getPutOutDate()));
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tRaterecord.getPutOutMoney(),null)){
                        providerCost.setPutOutMoney( Float.valueOf(String.valueOf(tRaterecord.getPutOutMoney())) );
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tRaterecord.getPutWorker(),null)){
                        providerCost.setPutWorker(tRaterecord.getPutWorker().toString());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tRaterecord.getIsPutOut(),null)){
                        providerCost.setState(tRaterecord.getIsPutOut());
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tRaterecord.getBase_FY(),null)){
                        providerCost.setBaseMoney( Float.valueOf(String.valueOf(tRaterecord.getBase_FY())));
                    }
                    if (AdscmBaseUtils.getInstance().isNotEmpty(tRaterecord.getTrafficFee(),null)){
                        providerCost.setTrafficFee(Float.valueOf(String.valueOf(tRaterecord.getTrafficFee())));
                    }

                    providerCostMapper.insert(providerCost);

                }
            }
        }
    }
}

